﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Security;
using System.Windows.Forms;

namespace BOTemplatesItemsWizards
{
    public partial class UsersRolesSettings : Form
    {
        private const string CnStr = "Data Source={0};Initial Catalog={1};Integrated Security=SSPI;";

        public UsersRolesSettings()
        {
            InitializeComponent();
        }

        private void Initialize()
        {
            LoadUsers();

            LoadRoles();
        }

        private void btnAssign_Click(object sender, EventArgs e)
        {
            if (ltbUsers.SelectedItems.Count > 0)
            {
                if (this.ltbRoles.SelectedItems.Count > 0)
                {
                    string m_userName = this.ltbUsers.SelectedItem.ToString();
                    string m_userRole = this.ltbRoles.SelectedItem.ToString();

                    ListViewItem m_association = new ListViewItem();
                    m_association.Name = string.Format("{0}_{1}", m_userName, m_userRole);
                    m_association.Text = m_userName;
                    m_association.SubItems.Add(new ListViewItem.ListViewSubItem() { Text = m_userRole });

                    if (!this.lstvwUsersRoles.Items.ContainsKey(m_association.Name))
                    {
                        this.lstvwUsersRoles.Items.Add(m_association);
                    }
                }
                else
                {
                    MessageBox.Show("Select a role...");
                }
            }
            else
            {
                MessageBox.Show("Select a user...");
            }

            this.ltbUsers.SelectedItems.Clear();
            this.ltbRoles.SelectedItems.Clear();
        }

        private void btnCancellaRuolo_Click(object sender, EventArgs e)
        {
            ListViewItem m_userToDelete = this.lstvwUsersRoles.SelectedItems[0];
            this.lstvwUsersRoles.Items.Remove(m_userToDelete);

            this.lstvwUsersRoles.Refresh();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.lstvwUsersRoles.Items.Count > 0)
                {
                    foreach (ListViewItem item in this.lstvwUsersRoles.Items)
                    {
                        Roles.AddUserToRole(item.Text, item.SubItems[1].Text);
                    }
                }

                MessageBox.Show("Roles assigned succesfully");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Errore: " + ex.ToString());
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void LoadUsers__()
        {
            MembershipUserCollection m_users = Membership.GetAllUsers();

            foreach (MembershipUser user in m_users)
            {
                this.ltbUsers.Items.Add(user.UserName);
            }
        }

        private void LoadRoles__()
        {
            string[] m_roles = Roles.GetAllRoles();

            this.ltbRoles.Items.AddRange(m_roles);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Initialize();
        }

        private void LoadUsers()
        {
            string m_cn = string.Format(CnStr, this.txtDataSource.Text, this.txtInitialCatalog.Text);

            string sql = "SELECT A.username FROM aspnet_Users AS A INNER JOIN aspnet_Applications AS B ON A.ApplicationId = B.ApplicationId ORDER BY A.username";

            SqlConnection mySqlConnection = new SqlConnection(m_cn);
            
            try
            {
                mySqlConnection.Open();
                SqlCommand cmd = new SqlCommand(sql, mySqlConnection);
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    this.ltbUsers.Items.Add(reader.GetValue(0));
                }
                reader.Close();
                cmd.Dispose();
                mySqlConnection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not open connection ! " + ex.ToString());
            }
        }

        private void LoadRoles()
        {
            string m_cn = string.Format(CnStr, this.txtDataSource.Text, this.txtInitialCatalog.Text);

            string sql = "SELECT A.roleName FROM aspnet_Roles AS A INNER JOIN aspnet_Applications AS B ON A.ApplicationId = B.ApplicationId ORDER BY A.roleName";

            SqlConnection mySqlConnection = new SqlConnection(m_cn);

            try
            {
                mySqlConnection.Open();
                SqlCommand cmd = new SqlCommand(sql, mySqlConnection);
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    this.ltbRoles.Items.Add(reader.GetValue(0));
                }
                reader.Close();
                cmd.Dispose();
                mySqlConnection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not open connection ! " + ex.ToString());
            }
        }
    }
}
